package com.musichord.core.helper;

import com.musichord.core.unit.chord.Chord;

public class ChordHelper {
	public static int NOTE_UNIT_DURATION = 2;
	public static int CHORD_UNIT_DURATION = 16;

	/* The strategy used in chord process */
	public static int CHORD_UPWARD = 1;
	public static int CHORD_DOWNWARD = 2;
	public static int CHORD_CLOSEST = 3;
	public static int CHORD_SMART = 4;

	/* Default going down */
	public static int CHORD_STRATEGY = CHORD_CLOSEST;

	public static void AdjustChord(Chord lastChord, Chord chord) {
		if (lastChord != null) {
			if (CHORD_STRATEGY == CHORD_DOWNWARD) {
				int low_pitch = chord.getLowestPitch();
				while (low_pitch > lastChord.getLowestPitch()) {
					chord.OctaveDown();
					low_pitch = chord.getLowestPitch();
				}
			} else if (CHORD_STRATEGY == CHORD_UPWARD) {
				int low_pitch = chord.getLowestPitch();
				while (low_pitch < lastChord.getLowestPitch()) {
					chord.OctaveUp();
					low_pitch = chord.getLowestPitch();
				}
			} else if (CHORD_STRATEGY == CHORD_CLOSEST) {
				Chord chord_up = chord.clone().OctaveUp();
				Chord chord_down = chord.clone().OctaveDown();
				int dist = Math.abs(lastChord.getLowestPitch()
						- chord.getLowestPitch());
				int dist_up = Math.abs(lastChord.getLowestPitch()
						- chord_up.getLowestPitch());
				int dist_down = Math.abs(lastChord.getLowestPitch()
						- chord_down.getLowestPitch());
				int min_dist = dist;
				if (dist_up < min_dist) {
					chord.setStandardPitch(chord_up.getStandardPitch());
					min_dist = dist_up;
				}
				if (dist_down < min_dist) {
					chord.setStandardPitch(chord_down.getStandardPitch());
					min_dist = dist_down;
				}
			} else if (CHORD_STRATEGY == CHORD_SMART) {

			}
		}
	}
}
